<template>
  <div>
    <div class="header">
      <el-select v-model="year" placeholder="请选择年份" @change="changeDate">
        <el-option v-for="item in yearList" :key="item" :value="item" />

      </el-select>
      <el-select v-model="month" placeholder="请选择月份" @change="changeDate">
        <el-option v-for="num in 12" :key="num" :value="num" />
      </el-select>
    </div>
    <el-calendar v-model="date">
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <template #dateCell="{ date }">
        <!--  -->
        <div class="date-content">
          {{ date.getDate() }}
          <span v-if=" isWeek(date) " class="rest">休</span>
        </div>

      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      year: 0,
      month: 1,
      yearList: [],
      date: new Date()
    }
  },

  created() {
    // 每当创建这个组件，我就获取当前时间
    const now = new Date()
    // 获得当前年，并赋值给this.year
    this.year = now.getFullYear()
    this.month = now.getMonth() + 1
    // 现在是2023,5年前是2023-5即：2018   后5年是2023+5即：2028
    for (let i = this.year - 5; i <= this.year + 5; i++) {
      this.yearList.push(i)
    }
  },

  methods: {
    isWeek(date) {
      // 这是获取星期几，但是它也是从0开始
      // 国外是星期天为一周的第一天所以相当于0就是星期天，其他的1就是星期一，2就是星期二以此类推
      const day = date.getDay()
      return (day === 0 || day === 6) && date.getMonth() + 1 === this.month
    },
    changeDate() {
      // 根据选完的年、月得到日历数据
      this.date = new Date(`${this.year}-${this.month}-1`)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-calendar__header {
    display: none;
  }
}

.header {
  text-align: right;
  .el-select {
    width: 100px;

    ::v-deep {
      .el-input__inner {
        height: 30px;
      }
    }
  }
}

::v-deep .el-calendar-day {
  height: auto;
}
::v-deep {
  .el-calendar-table__row td,
  .el-calendar-table tr td:first-child,
  .el-calendar-table__row td.prev {
    border: none;
  }
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
::v-deep .el-calendar__header {
  display: none;
}

::v-deep {
  .el-calendar-table {
    thead {
      th:nth-child(n + 6) {
        color: rgb(250, 124, 77);
        font-weight: 700;
      }
    }
  }
}
</style>
